package com.randi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.randi.common.annotation.LogAnnotation;
import com.randi.common.core.domain.Result;
import com.randi.common.core.domain.entity.SysUser;
import com.randi.system.domain.params.SysUserParam;
import com.randi.common.enums.BusinessType;
import com.randi.common.enums.ResultCode;
import com.randi.common.utils.page.PageHelperUtils;
import com.randi.system.domain.dto.RoleMenuDTO;
import com.randi.system.domain.dto.UserRoleDTO;
import com.randi.system.domain.vo.AuthMenu;
import com.randi.system.domain.vo.AuthMenuInfo;
import com.randi.system.service.ISysRoleMenuService;
import com.randi.system.service.ISysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ziye
 */
@RestController
@RequestMapping("system/role/allocated")
@Api(tags = "授权管理")
public class SysUserRoleController {

    @Resource
    private ISysUserRoleService iSysUserRoleService;

    @Resource
    private ISysRoleMenuService iSysRoleMenuService;

    /**
     * 分页获取授权用户列表
     *
     * @param roleId    角色ID
     * @param allocated 是否授权
     * @param userParam 检索参数
     * @return 用户列表
     */
    @PostMapping("/user/list")
    @ApiOperation(value = "获取授权用户列表", notes = "")
    @PreAuthorize("@auth.hasPermission('system:role:auth')")
    public Result selectAllocatedUserList(@RequestParam Long roleId,
                                          @RequestParam Boolean allocated,
                                          @RequestBody SysUserParam userParam) {

        PageInfo<SysUser> userList = iSysUserRoleService.selectAllocatedUserList(roleId, allocated, userParam);
        return Result.success().putPage(userList);
    }

    /**
     * 添加授权用户
     *
     * @param userRoleDTO UserRoleDTO
     * @return Result
     */
    @PostMapping("/user/add")
    @ApiOperation(value = "添加授权用户", notes = "")
    @PreAuthorize("@auth.hasPermission('system:role:auth')")
    @LogAnnotation(module = "授权管理", value = "添加授权用户", businessType = BusinessType.INSERT)
    public Result addAuthUsers(@Validated @RequestBody UserRoleDTO userRoleDTO) {
        int status = iSysUserRoleService.addAuthUsers(userRoleDTO);
        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_ROLE_AUTH_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_ROLE_AUTH_SUCCESS).put(status);
    }

    /**
     * 取消授权用户
     *
     * @param userRoleDTO UserRoleDTO
     * @return Result
     */
    @PostMapping("/user/cancel")
    @ApiOperation(value = "取消授权用户", notes = "")
    @PreAuthorize("@auth.hasPermission('system:role:auth')")
    @LogAnnotation(module = "授权管理", value = "取消授权用户", businessType = BusinessType.DELETE)
    public Result cancelAuthUsers(@Validated @RequestBody UserRoleDTO userRoleDTO) {
        int status = iSysUserRoleService.cancelAuthUsers(userRoleDTO);
        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_ROLE_AUTH_CANCEL_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_ROLE_AUTH_CANCEL_SUCCESS).put(status);
    }

    /**
     * 获取授权菜单树
     *
     * @return 授权菜单树
     */
    @GetMapping("/menu/info/role_{roleId}")
    @ApiOperation(value = "获取授权菜单树", notes = "")
    @PreAuthorize("@auth.hasPermission('system:role:auth')")
    public Result selectAllocatedMenuTree(@PathVariable Long roleId) {
        List<AuthMenu> authMenuList = iSysRoleMenuService.selectAllocatedMenuTree();
        List<Long> menuIds = iSysRoleMenuService.selectAllocatedMenu(roleId);

        return Result.success().put(new AuthMenuInfo(authMenuList, menuIds));
    }

    /**
     * 变更授权菜单权限
     *
     * @return 结果
     */
    @PostMapping("/menu/update")
    @ApiOperation(value = "变更授权菜单权限", notes = "")
    @PreAuthorize("@auth.hasPermission('system:role:auth')")
    @LogAnnotation(module = "授权管理", value = "变更菜单权限", businessType = BusinessType.UPDATE)
    public Result updateAuthMenu(@Validated @RequestBody RoleMenuDTO roleMenuDTO) {
        int status = iSysRoleMenuService.updateRoleMenu(roleMenuDTO);
        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_ROLE_AUTH_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_ROLE_AUTH_SUCCESS).put(status);
    }
}
